package com.itboye.textileexpres.top;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itboye.textileexpes.beans.GoodsTopBean;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.WebActivity;
import com.itboye.textileexpres.util.TimeFormat;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;
/**
 * 首页商品列表
 */
public class GoodsFragmentAdapter extends BaseAdapter {
	Context context;
	List<GoodsTopBean.A> list = new ArrayList<GoodsTopBean.A>();
	int type;
	private final int TYPE_AD = 0;
	private final int TYPE_1 = 1;
	private final int TYPE_3 = 2;
	OnClickListener deleteonclick;
	OnClickListener collectiononclick;

	public GoodsFragmentAdapter(Context context, List<GoodsTopBean.A> list,
			OnClickListener deleteonclick, OnClickListener collectiononclick) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.list = list;
		this.deleteonclick = deleteonclick;
		this.collectiononclick = collectiononclick;
	}

	public GoodsFragmentAdapter(Context context, int type) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.type = type;
	}

	public GoodsFragmentAdapter(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.list != null ? this.list.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if ("ad".equals(list.get(position).getType())) {
			return TYPE_AD;
		} else {
			if (list.get(position).getInfo().get(0).get_imgs() == null) {
				return TYPE_1;
			} else {
				return TYPE_3;
			}
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		ADViewHolder adHolder = null;
		type = getItemViewType(position);
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			switch (type) {
			case TYPE_1:
				convertView = inflater.inflate(R.layout.item_goods_1, parent,
						false);
				holder = new ViewHolder();
				holder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				holder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);
				holder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				holder.img_grid = (GridView) convertView
						.findViewById(R.id.img_grid);
				holder.context = (TextView) convertView
						.findViewById(R.id.context);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				convertView.setTag(holder);
				break;
			case TYPE_3:
				convertView = inflater.inflate(R.layout.item_goods_2, parent,
						false);
				holder = new ViewHolder();
				holder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.context = (TextView) convertView
						.findViewById(R.id.context);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				holder.img_grid = (GridView) convertView
						.findViewById(R.id.img_grid);
				holder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				holder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				holder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);

				holder.img1 = (ImageView) convertView.findViewById(R.id.img1);
				holder.img2 = (ImageView) convertView.findViewById(R.id.img2);
				holder.img3 = (ImageView) convertView.findViewById(R.id.img3);
				holder.lineImg = (LinearLayout) convertView
						.findViewById(R.id.lineImg);
				convertView.setTag(holder);
				break;
			case TYPE_AD:
				convertView = inflater.inflate(R.layout.item_goods_3, parent,
						false);
				adHolder = new ADViewHolder();
				adHolder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				adHolder.title = (TextView) convertView
						.findViewById(R.id.title);
				adHolder.context = (TextView) convertView
						.findViewById(R.id.context);
				adHolder.img = (ImageView) convertView.findViewById(R.id.img);
				adHolder.number = (TextView) convertView
						.findViewById(R.id.number);
				adHolder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				adHolder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);
				convertView.setTag(adHolder);
				break;
			default:
				break;
			}
		} else {
			switch (type) {
			case TYPE_1:
				holder = (ViewHolder) convertView.getTag();
				break;
			case TYPE_3:
				holder = (ViewHolder) convertView.getTag();
				break;
			case TYPE_AD:
				adHolder = (ADViewHolder) convertView.getTag();
				break;
			default:
				break;
			}
		}

		switch (type) {
		case TYPE_1:
			for (int i = 0; i < list.get(position).getInfo().size(); i++) {
				holder.context.setText(list.get(position).getInfo().get(i)
						.getSecondary_headlines());
				holder.delete_img.setTag(position);
				holder.title.setText(list.get(position).getInfo().get(i)
						.getName());
				holder.number
						.setText(context.getString(R.string.h56)
								+ ":"
								+ list.get(position).getInfo().get(i)
										.getProduct_code());
				holder.publish_time.setText(TimeFormat.DateFormat(list
						.get(position).getInfo().get(i).getCreatetime()));
				holder.collection_img.setTag(position);
				System.out.println("OLOLOLOLLOLLOO"
						+ list.get(0).getInfo().get(0).get_imgs());
				if (list.get(position).getInfo().get(0).get_imgs() == null) {
					holder.goods_img.setVisibility(View.VISIBLE);
				} else {
					XImageLoader.load(
							GlobalConfig.IMAGE_DOUBLE
									+ list.get(position).getInfo().get(0)
											.get_imgs().get(0),
							holder.goods_img);
				}
			}
			if ("0".equals(list.get(position).getInfo().get(0).getIs_fav())) {
				holder.collection_img
						.setImageResource(R.drawable.home_collection_img);
			} else {
				holder.collection_img
						.setImageResource(R.drawable.collect_collection);
			}
			holder.delete_img.setOnClickListener(deleteonclick);
			holder.collection_img.setOnClickListener(collectiononclick);
			break;
		case TYPE_3:
			for (int i = 0; i < list.get(position).getInfo().size(); i++) {
				holder.context.setText(list.get(position).getInfo().get(i)
						.getSecondary_headlines());
				holder.delete_img.setTag(position);
				holder.title.setText(list.get(position).getInfo().get(i)
						.getName());
				holder.number
						.setText(context.getString(R.string.h56)
								+ ":"
								+ list.get(position).getInfo().get(i)
										.getProduct_code());
				holder.publish_time.setText(TimeFormat.DateFormat(list
						.get(position).getInfo().get(i).getCreatetime()));
				holder.collection_img.setTag(position);
				List<String> arrayImage = list.get(position).getInfo().get(i)
						.get_imgs();

				if (arrayImage != null && arrayImage.size() > 0) {
					holder.lineImg.setVisibility(View.GONE);
					if (arrayImage.size() > 0) {
						XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
								+ arrayImage.get(i), holder.img1);
					}
					if (arrayImage.size() > 1) {
						XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
								+ arrayImage.get(i), holder.img2);
					}
					if (arrayImage.size() > 2) {
						XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
								+ arrayImage.get(i), holder.img3);
					}
				} else {
					holder.lineImg.setVisibility(View.GONE);
				}

				/***
				 * 使用gridview的
				 */

				holder.img_grid.setAdapter(new NoScrollGridAdapter(context,
						arrayImage));
				if ("0".equals(list.get(position).getInfo().get(i).getIs_fav())) {
					holder.collection_img
							.setImageResource(R.drawable.home_collection_img);
				} else {
					holder.collection_img
							.setImageResource(R.drawable.collect_collection);
				}
				final String pid = list.get(position).getInfo().get(i).getId();
				holder.title.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(context, GoodsDetail.class);
						intent.putExtra("pid", pid);
						context.startActivity(intent);
					}
				});
				holder.context.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(context, GoodsDetail.class);
						intent.putExtra("pid", pid);
						context.startActivity(intent);
					}
				});
				holder.img_grid.setTag(position);
				holder.img_grid
						.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
								Intent intent = new Intent(context,
										GoodsDetail.class);
								intent.putExtra("pid", pid);
								context.startActivity(intent);
							}
						});
			}

			holder.delete_img.setOnClickListener(deleteonclick);
			holder.collection_img.setOnClickListener(collectiononclick);

			break;
		case TYPE_AD:
			for (int i = 0; i < list.get(position).getInfo().size(); i++) {
				if (list.get(position).getInfo().get(i).getImg() != null) {
					XImageLoader.load(
							GlobalConfig.IMAGE_DOUBLE
									+ list.get(position).getInfo().get(i)
											.getImg(), adHolder.img);
				}
				adHolder.delete_img.setTag(position);
				adHolder.collection_img.setTag(position);
				adHolder.delete_img.setVisibility(View.GONE);
				adHolder.collection_img.setVisibility(View.GONE);
				adHolder.publish_time.setVisibility(View.GONE);
				adHolder.delete_img.setOnClickListener(deleteonclick);
				adHolder.collection_img.setOnClickListener(collectiononclick);
				adHolder.context.setText(list.get(position).getInfo().get(i)
						.getNotes());
				adHolder.number.setText("");
				adHolder.title.setText(list.get(position).getInfo().get(i)
						.getTitle());
				adHolder.publish_time.setText(TimeFormat.DateFormat(list
						.get(position).getInfo().get(i).getCreatetime()));
				adHolder.img.setTag(i);
				adHolder.img.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = null;
						if ("6070".equals(list.get(position).getInfo()
								.get((int) v.getTag()).getUrl_type())) {
							intent = new Intent(context, WebActivity.class);
							intent.putExtra("url", list.get(position).getInfo()
									.get((int) v.getTag()).getUrl());
						} else if ("6071".equals(list.get(position).getInfo()
								.get((int) v.getTag()).getUrl_type())) {
							intent = new Intent(context, GoodsDetail.class);
							intent.putExtra("pid", list.get(position).getInfo()
									.get((int) v.getTag()).getUrl());
						}
						context.startActivity(intent);
					}
				});
			}

			break;
		default:
			break;

		}
		return convertView;
	}

	class ViewHolder {
		GridView img_grid;
		TextView publish_time, title, context, number;
		ImageView collection_img, delete_img, goods_img;

		ImageView img1, img2, img3;
		LinearLayout lineImg;
	}

	class ADViewHolder {
		TextView publish_time, title, context, number;
		ImageView collection_img, delete_img, img;
	}
}
